package com.tqfframe.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: 唐祺峰
 * @service 在调用服务之前执行，如果异常，则进行拦截
 * @Date: 2022/2/8 0008 14:30
 * @Version 1.0
 */
public class ErrorExceptionFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(ErrorExceptionFilter.class);

    @Override
    public String filterType() {
        //异常过滤器
        return "error";
    }

    @Override
    public int filterOrder() {
        //优先级，数字越大，优先级越低
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        //是否执行该过滤器，true代表需要过滤
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();

        log.info("进入异常过滤器");

        System.out.println(ctx.getResponseBody());

        ctx.setResponseBody("出现异常");

        return null;

    }

}